
package com.cloudsigns.factorymanager2.engineering.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.activity.ReceiveEngineeringBatchInstallationOrderAcitivity;
import com.cloudsigns.factorymanager2.engineering.activity.ReceiveEngineeringConstructOrderActivity;
import com.cloudsigns.factorymanager2.engineering.activity.ReceiveEngineeringInstallOrderActivity;
import com.cloudsigns.factorymanager2.engineering.activity.ReceiveEngineeringOrderListActivity;
import com.cloudsigns.factorymanager2.engineering.activity.ReceiveMaintenanceOrderActivity;
import com.cloudsigns.factorymanager2.engineering.activity.ReceivePatrolnOrderActivity;
import com.cloudsigns.factorymanager2.engineering.activity.ReceivePublicationOrderActivity;
import com.cloudsigns.factorymanager2.engineering.adapter.WattingToReceiveEngineeringOrderAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.event.ReceiveEngineringOrderListEvent;
import com.cloudsigns.factorymanager2.project.activity.ReceiveSurveyOrderActivity;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 等待接收
 * Created by tony on 2018/1/14.
 */

public class WattingToReceiveEngineeringOrderFragment extends Fragment {
    private final String TAG = WattingToReceiveEngineeringOrderFragment.class.getSimpleName();
    private ListView lv;
    private WattingToReceiveEngineeringOrderAdapter adapter;
    private List<OrderParentBean> orderParentBeenList = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_watting_to_receive_engineering_order, null);
        lv = view.findViewById(R.id.lv);

        adapter = new WattingToReceiveEngineeringOrderAdapter(getActivity(), TAG);
        adapter.setData(orderParentBeenList);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent mIntent;
                OrderParentBean order = orderParentBeenList.get(position);
                if (null != order.getCategory()) {
                    CommonEnum.EngineeringORderCategory category = order.getCategory();
                    // 工程安装
                    if (category == CommonEnum.EngineeringORderCategory.INSTALL) {
                        mIntent = new Intent(getActivity(), ReceiveEngineeringInstallOrderActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, order);
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                    }
                    // 批量安装
                    else if (category == CommonEnum.EngineeringORderCategory.BATCH_INSTALL) {
                        mIntent = new Intent(getActivity(), ReceiveEngineeringBatchInstallationOrderAcitivity.class);
                        mIntent.putExtra("type","WattingToReceiveEngineeringOrderFragment");
                        mIntent.putExtra(ConstantUtil.KEY_1, order);
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                    }
                    // 工程施工
                    else if (category == CommonEnum.EngineeringORderCategory.CONSTRUCT) {
                        mIntent = new Intent(getActivity(), ReceiveEngineeringConstructOrderActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, order);
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                    }
                    // 维修单
                    else if (category == CommonEnum.EngineeringORderCategory.MAINTAIN) {
                        mIntent = new Intent(getActivity(), ReceiveMaintenanceOrderActivity.class);
                        mIntent.putExtra("type","WattingToReceiveEngineeringOrderFragment");
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                    }
                    // 上刊工单
                    else if (category == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                        mIntent = new Intent(getActivity(), ReceivePublicationOrderActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, order);
                        mIntent.putExtra(ConstantUtil.KEY_2, "publication");
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                    }
                    // 巡查工单
                    else if (category == CommonEnum.EngineeringORderCategory.PATROL) {
                        mIntent = new Intent(getActivity(), ReceivePatrolnOrderActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, order);
                        mIntent.putExtra(ConstantUtil.KEY_2, "patrol");
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                    }
                    // 现场勘察
                    else if (category == CommonEnum.EngineeringORderCategory.SURVEY) {
                        mIntent = new Intent(getActivity(), ReceiveSurveyOrderActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, order);
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                    }
                }
            }
        });
        return view;
    }

    public void initData(List<OrderParentBean> engineeringOrderBeanList) {
        if (engineeringOrderBeanList != null) {
            orderParentBeenList.clear();
            orderParentBeenList.addAll(engineeringOrderBeanList);
            adapter.setData(orderParentBeenList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            switch (requestCode) {
                case ConstantUtil.START_ACTIVITY_KEY_1:
                    EventBus.getDefault().post(new ReceiveEngineringOrderListEvent());
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
